package regist;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import regist.aliutil.Config;
import regist.aliutil.SignatureUtils;

public class AliDeviceHttp {
	private static Logger logger = LoggerFactory.getLogger(AliDeviceHttp.class);

	public static Map<String, String> getInitParameter() {
		String timestamp = LocalDateTimeUtil.format(LocalDateTime.now(ZoneOffset.UTC), "yyyy-MM-dd'T'HH:mm:ss'Z'");
		Map<String, String> map = new HashMap<String, String>();
		// 公共参数
		map.put("Format", "JSON");
		map.put("Version", "2018-01-20");
		map.put("AccessKeyId", PropertyConfig.getStr("accessKey"));
		map.put("SignatureMethod", "HMAC-SHA1");
		map.put("Timestamp", timestamp);
		map.put("SignatureVersion", "1.0");
		map.put("SignatureNonce", RandomUtil.randomNumbers(14));
		map.put("RegionId", PropertyConfig.getStr("regionId"));
		return map;
	}

	public static JSONObject deviceQuery(String deviceName) {
		JSONObject data = new JSONObject();
		Map<String, String> map = getInitParameter();
		// 请求参数
		map.put("Action", "QueryDeviceDetail");
		if (StringUtils.isNotEmpty(PropertyConfig.getStr("instanceId"))) {
			map.put("IotInstanceId", PropertyConfig.getStr("instanceId"));
		}
		map.put("DeviceName", deviceName);
		map.put("ProductKey", PropertyConfig.getStr("productKey"));

		try {
			String signature = SignatureUtils.generate("GET", map, PropertyConfig.getStr("accessKeySecret"));
			map.put("Signature", signature);
			Map<String, Object> httpParamMap = new HashMap<String, Object>(map);
			String result = HttpUtil.get(Config.API_ADDRESS, httpParamMap);
			System.out.println("result=====" + result);
			JSONObject queryJson = (JSONObject) JSONObject.parse(result);
			if (queryJson.getBoolean("Success")) {
				data = queryJson.getJSONObject("Data");
			}
		} catch (Exception e) {
			logger.error("物联网平台设备查询失败", e);
		}
		return data;
	}

	public static JSONObject deviceRegist(String deviceName) {
		JSONObject data = new JSONObject();
		Map<String, String> map = getInitParameter();

		// 请求参数
		map.put("Action", "RegisterDevice"); // 注册设备
		if (StringUtils.isNotEmpty(PropertyConfig.getStr("instanceId"))) {
			map.put("IotInstanceId", PropertyConfig.getStr("instanceId"));
		}
		map.put("DeviceName", deviceName);
		map.put("ProductKey", PropertyConfig.getStr("productKey"));

		try {
			String signature = SignatureUtils.generate("GET", map, PropertyConfig.getStr("accessKeySecret"));
			map.put("Signature", signature);
			Map<String, Object> httpParamMap = new HashMap<String, Object>(map);
			String result = HttpUtil.get(Config.API_ADDRESS, httpParamMap);
			System.out.println("result====" + result);
			JSONObject queryJson = (JSONObject) JSONObject.parse(result);
			if (queryJson.getBoolean("Success")) {
				data = queryJson.getJSONObject("Data");
			}
		} catch (Exception e) {
			logger.error("物联网平台设备查询失败", e);
		}
		return data;
	}

	public static void main(String[] args) {
		deviceRegist("123456789");
	}
}
